<?php
namespace Home\Controller;
use Home\Aip\User;
use Lib\PasswordHash;

class LoginController extends CommonController{

	/*
	*会员登录
	*/
    public function login(){	
		$this->display();		
    }
	
	/*
	*会员登录
	*/
    public function loginon(){
				
		if (IS_POST) {

			if(!I('post.code')) {
				if (I('post.type')==='store_joinin') {
					$this->error('验证码不能为空！','/store_joinin');
				} else {
					$this->ajaxReturn(array('error'=>'验证码不能为空！')) ;
				}
			}
			
			if (!$this->check_verify(I('post.code'))) {
				if (I('post.type')==='store_joinin') {
					$this->error('验证码错误！','/store_joinin');
				} else {
					$this->ajaxReturn(array('error'=>'验证码错误！')) ;
				}				
			}
			//dump(I('post.code'));die;
			
			$user = new User();
			$uinfo = $user->_login();
			
			if (I('post.type')==='store_joinin') {				
				if ($uinfo=='boboid') {
					 $this->redirect('/store_joinin','登录成功');					 
				} elseif ($uinfo=='ok') {
					$this->redirect('/store_joinin','登录成功');
				} else{
					$this->error($uinfo,'/store_joinin');
				}
				
			} else {
				if ($uinfo=='boboid') {
					$this->ajaxReturn(array('info'=>1));
				} elseif ($uinfo=='ok') {
					$this->ajaxReturn(array('info'=>1));
				} else{
					$this->ajaxReturn(array('error'=>$uinfo));
				}				
			}		
		} else {
			$this->display();
		}		
    }

	/*
	*退出登录
	*/		
	public function getout(){
		$cache = 'user_'.$_SESSION['user_id'];
		S($cache,null);
		outClear();
		setcookie('ceshi001',null);
		cookie('_cart',null) ;
	    cookie('_cart_goods_num',null) ;
		session(null);
		$this->redirect('/login','退出登录');
	}

	/*
	*检测会员账号
	*/	
    public function checkuname(){
		$UN = new User();
		$where ="username='%s'";
		$data  = array(I('post.uno'));
		$userinfo = $UN -> _getUserInfo($where,$data);	
		$this->ajaxReturn(array('member'=>$userinfo));
    }	
	
	/*
	*
	*验证码
	*/

	public function verify(){
		ob_clean();
		$Verify = new  \Think\Verify();// 设置验证码字符为纯数字
		$Verify->fontttf = '5.ttf'; 
		$Verify->fontSize = 50;
		$Verify->length   = 4;
		$Verify->useNoise = false;
		//$Verify->useCurve = false;
		$Verify->codeSet = '0123456789'; 
		$Verify->entry();

	}
		
    //检验 验证码
    public function check_verify($code, $id = ''){
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
    }	
	
	/*
	*会员注册
	*/	
    public function shop_reg(){
	
		if (IS_POST && I('post.form_submit')=="ok") {
			//dump(I('post.'));die;
			if(!I('post.code')) {
				if (I('post.ref_url')==='reg_pc') {
					die ("<script language=javascript>alert('验证码不能为空！');javascript:history.go(-1);</script>");
				}
			}
			
			if (!$this->check_verify(I('post.code'))) {
				if (I('post.ref_url')==='reg_pc') {
					die ("<script language=javascript>alert('验证码错误！');javascript:history.go(-1);</script>");
				}				
			}
			//dump(I('post.code'));die;
			$ip = get_client_ip();
			$hash = shopHash('regHash'.$ip,I('post.formhash'));
			if ($hash) {
				$user = new User();
				$uinfo = $user->_reg_user();				
			} else {
				die ("<script language=javascript>alert('不能重复提交数据！');javascript:history.go(-1);</script>");
			} 
			
			if (I('post.ref_url')==='reg_pc') {	
				//会员账户注册
				if ($uinfo=='boboid') {
					 $this->error('会员注册成功！','/login');
					
				} else{
					die ("<script language=javascript>alert('".$uinfo."');javascript:history.go(-1);</script>");
				}				
			}
			
		} else {
			$this->display();
		}
    }
	
	/*
	*检测会员账号、邮箱
	*/	
    public function check_user(){
		$UN = new User();
		unset($where);
		unset($data);
		unset($userinfo);	
		$where ="username='%s'";
		$data  = array(I('post.user_name'));
		$userinfo = $UN -> _getUserInfo($where,$data);	
		if ($userinfo) {
			return '会员已存在';
		} else {
			unset($where);
			unset($data);
			unset($userinfo);			
			$where ="email='%s'";
			$data  = array(I('post.email'));
			$userinfo = $UN -> _getUserInfo($where,$data);	
			if ($userinfo) {
				return '邮箱已存在';
			} else {
				return true;
			}
		}
		
    }
	
}